using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
using System;
using System.Threading.Tasks;
using Avalonia.Controls.ApplicationLifetimes;

namespace MySoftLab.DapFlash.Views
{
    public class InputDialog
    {
        public static async Task<(bool Success, string Value1, string Value2)> ShowDialog(
            string title,
            string label1,
            string label2,
            string initialValue1 = "",
            string initialValue2 = "",
            string okText = "确定",
            string cancelText = "取消")
        {
            var tcs = new TaskCompletionSource<(bool, string, string)>();
            
            var textBox1 = new TextBox
            {
                Text = initialValue1,
                Margin = new Thickness(0, 5, 0, 15),
                MinWidth = 250
            };
            
            var textBox2 = new TextBox
            {
                Text = initialValue2,
                Margin = new Thickness(0, 5, 0, 15),
                MinWidth = 250
            };
            
            var okButton = new Button
            {
                Content = okText,
                MinWidth = 80,
                Margin = new Thickness(0, 0, 10, 0)
            };
            
            var cancelButton = new Button
            {
                Content = cancelText,
                MinWidth = 80
            };
            
            var buttonPanel = new StackPanel
            {
                Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Right,
                Children = { okButton, cancelButton }
            };
            
            var panel = new StackPanel
            {
                Margin = new Thickness(20),
                Children =
                {
                    new TextBlock
                    {
                        Text = label1,
                        Margin = new Thickness(0, 0, 0, 5)
                    },
                    textBox1,
                    new TextBlock
                    {
                        Text = label2,
                        Margin = new Thickness(0, 0, 0, 5)
                    },
                    textBox2,
                    buttonPanel
                }
            };
            
            var window = new Window
            {
                Title = title,
                Content = panel,
                SizeToContent = SizeToContent.WidthAndHeight,
                CanResize = false,
                WindowStartupLocation = WindowStartupLocation.CenterOwner
            };
            
            okButton.Click += (s, e) =>
            {
                tcs.SetResult((true, textBox1.Text, textBox2.Text));
                window.Close();
            };
            
            cancelButton.Click += (s, e) =>
            {
                tcs.SetResult((false, "", ""));
                window.Close();
            };
            
            window.Closed += (s, e) =>
            {
                if (!tcs.Task.IsCompleted)
                {
                    tcs.SetResult((false, "", ""));
                }
            };
            
            if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                await window.ShowDialog(desktop.MainWindow);
            }
            
            return await tcs.Task;
        }
    }
} 